package com.takeeasy.platform.config;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.takeeasy.platform.cache.ConfigCache;
import com.takeeasy.platform.exception.ConfigFileParseException;
import com.takeeasy.platform.util.FileUtil;
import com.takeeasy.platform.util.URLUtil;

public class ConfigParser {
	private static final String configName = "setting.xml";
	private static final Logger logger = LoggerFactory.getLogger(ConfigParser.class);

	public ConfigParser() {
		logger.debug("获取服务器配置...");
	}

	public void loadConfig() throws ConfigFileParseException {
		SAXBuilder builder = new SAXBuilder();
		String configPath = FileUtil.getClassRootPath() + FileUtil.FILE_SEPARATOR + configName;
		logger.debug("获取服务器配置文件：" + configPath);
		Document document;
		try {
			document = builder.build(new File(configPath));
			Element root = document.getRootElement();
			Element database = (Element) root.getChild("database");
			parseDatabase(database);
			Element common = (Element) root.getChild("common");
			parserCommon(common);
		} catch (JDOMException e) {
			throw new ConfigFileParseException(e, "配置解析错误！", "");
		} catch (IOException e) {
			throw new ConfigFileParseException(e, "配置加载错误！", "");
		}
	}

	private void parserCommon(Element common) {
		logger.debug("解析通用配置");
		Map<String, String> commonConfig = new HashMap<String, String>();
		@SuppressWarnings("unchecked")
		List<Element> commonEleList = common.getChildren();
		commonEleList.forEach(commonEle -> {
			String eleName = commonEle.getName();
			String eleValue = commonEle.getText();
			logger.debug("通用配置：" + eleName + ",值：" + eleValue);
			commonConfig.put(eleName, eleValue);
		});
		ConfigCache.addConfig(ConfigCache.CONFIG_COMMON, commonConfig);
	}

	private void parseDatabase(Element database) {
		logger.debug("解析数据库配置！");
		Map<String, String> databaseConfig = new HashMap<String, String>();
		Element className = database.getChild("driverClassName");
		databaseConfig.put("className", className.getText());
		Element username = database.getChild("username");
		databaseConfig.put("username", username.getText());
		Element password = database.getChild("password");
		databaseConfig.put("password", password.getText());
		Element maxIdle = database.getChild("maxIdle");
		databaseConfig.put("maxIdle", maxIdle.getText());
		Element maxWait = database.getChild("maxWait");
		databaseConfig.put("maxWait", maxWait.getText());
		Element maxActive = database.getChild("maxActive");
		databaseConfig.put("maxActive", maxActive.getText());
		Element url = database.getChild("url");
		Element href = url.getChild("href");
		@SuppressWarnings("unchecked")
		List<Element> params = url.getChildren("param");
		String hrefText = href.getTextTrim();
		for (Element param : params) {
			hrefText = URLUtil.URLParameterAdd(hrefText, param.getAttributeValue("key"), param.getText());
		}
		databaseConfig.put("url", hrefText);
		ConfigCache.addConfig(ConfigCache.CONFIG_DATABASE, databaseConfig);

	}
	//
	// public static void main(String[] args) throws ConfigFileParseException {
	// ConfigParser parser = new ConfigParser();
	// parser.loadConfig();
	// System.out.println(Config.config);
	// }
}
